package com.fromdev.automation;

import java.util.Iterator;
import java.util.List;

import com.google.common.base.Strings;
import com.thoughtworks.selenium.Selenium;

public class SeleniumUtil {

	public static boolean doClick(Selenium selenium,  List elmentXpathList) {
		for (Iterator iterator = elmentXpathList.iterator(); iterator.hasNext();) {
			String elementXpath = (String) iterator.next();
			if((!Strings.isNullOrEmpty(elementXpath)) &&  doClick(selenium,elementXpath)) {
				return true;
			}
		}
		return false;
	}

	public static boolean doClick(Selenium selenium, String elementXpath) {
		int sleepTime = TimeUtil.getTypingTimeSeconds();
		try {			
			if (selenium.isElementPresent(elementXpath)) {
				selenium.click(elementXpath);
				selenium.waitForPageToLoad("30000");				
				System.out.println("Going to sleep for " + sleepTime);
				Thread.sleep(sleepTime);
				return true;
			}
		} catch (Exception e) {
			System.out.println("Cant find elementXpath: " + elementXpath);
		}
		return false;
	}
}
